Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.Closeable;
18: import cz.cvut.kbss.ontodriver.Connection;
19: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
20: import cz.cvut.kbss.ontodriver.config.DriverConfigParam;
21: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
22: import cz.cvut.kbss.ontodriver.config.ConfigurationParameter;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.owlapi.config.OwlapiConfigParam;
25: import cz.cvut.kbss.ontodriver.owlapi.connector.ConnectorFactory;
26: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
27: import cz.cvut.kbss.ontodriver.owlapi.list.OwlapiLists;
28:
29: import java.util.*;
30:
31: class OwlapiDriver implements Closeable, ConnectionListener {
32:
33: private static final List<ConfigurationParameter> CONFIGS = Arrays
34: .asList(DriverConfigParam.AUTO_COMMIT, DriverConfigParam.MODULE_EXTRACTION_SIGNATURE,
35: DriverConfigParam.REASONER_FACTORY_CLASS,
36: OwlapiConfigParam.IRI_MAPPING_DELIMITER, OwlapiConfigParam.MAPPING_FILE_LOCATION,
37: OwlapiConfigParam.WRITE_ON_COMMIT);
38:
39: private final DriverConfiguration configuration;
40: private volatile boolean open = true;
41:
42: private final ConnectorFactory connectorFactory;
43: private final Set<OwlapiConnection> openConnections = new HashSet<>();
44:
45: OwlapiDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
46: this.configuration = new DriverConfiguration(storageProperties);
47: configuration.addConfiguration(properties, CONFIGS);
48: this.connectorFactory = ConnectorFactory.createFactory();
49: }
50:
51: @Override
52: public synchronized void close() throws OntoDriverException {
53: if (!open) {
54: return;
55: }
56: for (OwlapiConnection c : openConnections) {
57: try {
58: c.removeListener();
59: c.close();
60: } catch (Exception e) {
61: throw new OwlapiDriverException(e);
62: }
63: }
64: connectorFactory.close();
65: this.open = false;
66: }
67:
68: @Override
69: public boolean isOpen() {
70: return open;
71: }
72:
73: Connection acquireConnection() throws OntoDriverException {
74: assert open;
75: final OwlapiAdapter adapter = new OwlapiAdapter(connectorFactory.getConnector(configuration));
76: final OwlapiConnection c = new OwlapiConnection(adapter);
77: c.setTypes(new OwlapiTypes(adapter, c::ensureOpen, c::commitIfAuto));
78: c.setProperties(new OwlapiProperties(adapter, c::ensureOpen, c::commitIfAuto));
79: c.setLists(new OwlapiLists(adapter, c::ensureOpen, c::commitIfAuto));
80: openConnections.add(c);
81: c.setListener(this);
82: return c;
83: }
84:
85: synchronized void reloadData() throws OwlapiDriverException {
86: assert open;
87: connectorFactory.reloadData();
88: }
89:
90: @Override
91: public void connectionClosed(Connection connection) {
92: openConnections.remove(connection);
93: }
94: }